package com.fahai.web.controller;

import com.fahai.common.PageVo;
import com.fahai.utils.json.JsonResult;
import com.fahai.web.entity.*;
import com.fahai.web.service.RoleMenuService;
import com.fahai.web.service.SysGroupService;
import com.fahai.web.service.SysUserService;
import com.fahai.web.service.UserInfoService;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by sky on 17/5/10.
 * 用户部门管理
 */
@Controller
@EnableGlobalMethodSecurity(prePostEnabled = true)
@RequestMapping("/sysuser")
public class SysUserController extends BaseController {
    static Logger logger = LoggerFactory.getLogger(SysUserController.class);

    @Autowired
    SysUserService sysUserService;

    @Autowired
    SysGroupService sysGroupService;


    @Autowired
    RoleMenuService roleMenuService;


    @Autowired
    UserInfoService userInfoService;

    /**
     * 返回账号权限列表数据
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','账号管理-0','账号管理-1')")
    @RequestMapping(value = "/usermanage", method = RequestMethod.GET)
    public String enterSysUserList(Model model) {

        List<UserGroup> userGroupList = sysGroupService.findUserGroupList();
        List<UserRole> userRoleList = sysUserService.findAllUserRole();
        List<Menu> menuList = roleMenuService.findAllMenu();
        model.addAttribute("userRoleList",userRoleList);
        model.addAttribute("userGroupList",userGroupList);       //公司部门
        model.addAttribute("menuList",menuList);       //公司部门
        model.addAttribute("selectTab","账号管理");
        return "account_manage";
    }

    /**
     * 添加新用户角色权限
     * @param sysUser
     * @return
     */
    @PreAuthorize("hasAuthority('ADMIN') or hasRole('账号管理-0')")
    @RequestMapping(value = "/saveSysUser",method = RequestMethod.GET)
    public @ResponseBody
    JsonResult saveSysUser(SysUser sysUser){


        if(sysUserService.selectUserByNameCount(sysUser.getUserName())==0){

            sysUser.setQdId(1);
            sysUserService.saveSysUser(sysUser);

            return ok("添加成功");

        }else{

            return fail("账户已存在");

        }


    }

    /**
     * 用户管理查询
     * @param pageVo
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','账号管理-0','账号管理-1')")
    @RequestMapping(value = "/accountQuery",method = RequestMethod.GET)
    public @ResponseBody
    JsonResult accountQuery( PageVo pageVo){
        PageVo pageVoData = sysUserService.selectUserList(pageVo);
        return ok("成功" ,     pageVoData);
    }

    /**
     * 编辑用户保存
     * @param sysUser
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','账号管理-0')")
    @RequestMapping(value = "/updateSysUser",method = RequestMethod.GET)
    public @ResponseBody
    JsonResult updateSysUser(SysUser sysUser){


        sysUserService.updateSysUser(sysUser);

        return ok("修改成功");
    }


    /**
     * 查看隶属的组权限列表
     * @param pageVo
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','账号管理-0','账号管理-1')")
    @RequestMapping(value = "/getRoleMenuList",method = RequestMethod.GET)
    public @ResponseBody
    JsonResult getRoleMenuList(PageVo pageVo){


       pageVo=roleMenuService.selectRoleMenuList(pageVo);

        return ok("查询成功",pageVo);
    }



    /**
     * 查看一个隶属的组拥有的权限
     * @param roleId
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','账号管理-0','账号管理-1')")
    @RequestMapping(value = "/getRoleMenu",method = RequestMethod.GET)
    public @ResponseBody
    JsonResult getRoleMenu(Integer roleId){


       List<Map<String,Object>> getData=roleMenuService.selectRoleMenuOne(roleId);

        return ok("查询成功",getData);
    }



    /**
     * 修改用户角色权限
     * @param roleId
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','账号管理-0')")
    @RequestMapping(value = "/updareRoleMenu",method = RequestMethod.GET)
    public @ResponseBody
    JsonResult updareRoleMenu(Integer roleId,String menuRole){

        List<RoleMenu> list=new ArrayList<RoleMenu>();

        if(menuRole!=null&&!"".equals(menuRole)){

            Gson gson = new Gson();

            Map<String, String> result = gson.fromJson(menuRole, Map.class);




            for (String key : result.keySet()) {

                String value = result.get(key);

                 RoleMenu rm=new RoleMenu();
                 rm.setMenuId(Integer.parseInt(key));
                 rm.setRoleId(roleId);
                 rm.setRole(Integer.parseInt(value));

                list.add(rm);

            }


        }

        roleMenuService.updateRoleMenuOne(roleId,list);

        return ok("修改成功");
    }


    /**
     * 新增用户角色
     * @param roleName
     * @param menuRole
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','账号管理-0')")
    @RequestMapping(value = "/addRoleMenu",method = RequestMethod.GET)
    public @ResponseBody
    JsonResult addRoleMenu(String roleName,String menuRole){

        List<RoleMenu> list=new ArrayList<RoleMenu>();

        if(menuRole!=null&&!"".equals(menuRole)){

            Gson gson = new Gson();

            Map<String, String> result = gson.fromJson(menuRole, Map.class);




            for (String key : result.keySet()) {

                String value = result.get(key);

                RoleMenu rm=new RoleMenu();
                rm.setMenuId(Integer.parseInt(key));

                rm.setRole(Integer.parseInt(value));

                list.add(rm);

            }


        }

        roleMenuService.addRoleMenuOne(roleName,list);

        return ok("新增成功");
    }


    /**
     * 查看增量图表
     * @param regiserTime
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','数据分析-0','数据分析-1')")
    @RequestMapping(value = "/getUserChart",method = RequestMethod.GET)
    public @ResponseBody
    JsonResult getUserChart(String regiserTime){

       List<Map<String,Object>>  getData=userInfoService.selectChartForHours(regiserTime);

        return ok("查询成功",getData);
    }



}


